package org.bing.engine.utility.helper;

import java.io.InputStream;

import org.bing.engine.common.logging.Log;
import org.bing.engine.common.logging.LogFactory;


public class StreamHelper {

	private static final Log logger = LogFactory.getLog(StreamHelper.class);

	public static String readTextFromStream(InputStream ins) {
		StringBuilder sb = new StringBuilder(4096);
		byte[] buf = new byte[4096];
		int num = 0;
		try {
			while ((num = ins.read(buf)) != -1) {
				sb.append(new String(buf, 0, num));
			}
			return sb.toString();
		} catch (Exception e) {
			logger.error("Read text from stream error!", e);
			throw new RuntimeException(e);
		} finally {
			try {
				ins.close();
			} catch (Exception e) {
				logger.error("Close stream error!", e);
				ins = null;
			}
		}

	}

	public static StringBuilder readTextBuilderFromStream(InputStream ins) {
		StringBuilder sb = new StringBuilder(4096);
		byte[] buf = new byte[4096];
		int num = 0;
		try {
			while ((num = ins.read(buf)) != -1) {
				sb.append(new String(buf, 0, num));
			}
			return sb;
		} catch (Exception e) {
			logger.error("Read text from stream error!", e);
			throw new RuntimeException(e);
		} finally {
			try {
				ins.close();
			} catch (Exception e) {
				logger.error("Close stream error!", e);
				ins = null;
			}
		}

	}
}
